package com.zzq.my.jetpacks.jectpackapp.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.divider.GridSpaceItemDecoration
import com.zzq.my.jetpacks.util.dp2px

/*
  * ================================================
  * 描述：
  * 作者：zhuangzeqin
  * 时间: 2023/4/20 11:10
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * 不要在循环模式下使用{@link RecyclerView.ViewHolder#getAdapterPosition} 方法获取position，这个方法可能会返回一个不正确的position
    如果你需要通过getAdapterPosition()方法获取position，可参考如下代码:
     int adapterPosition = holder.getAdapterPosition();
     int realPosition = BannerUtils.getRealPosition(isCanLoop, adapterPosition, mList.size());
  * ================================================
  */
class BaseBannerMenuAdapter : BaseBannerAdapter<List<String>>() {
    //todo 1通过接口回传
    private var mOnItemClickListener: OnItemClickCallBack? = null

    //todo 1通过接口回传
    fun setOnItemClickListener(listener: OnItemClickCallBack?) {
        this.mOnItemClickListener = listener
    }

    //todo DSL 3
    fun setOnItemClickListenerDsl(listener: (BaseQuickAdapter<*, *>, View, Int) -> Unit) {
        this.mOnItemClickCallBck = listener
    }
    //todo DSL 3
    private var mOnItemClickCallBck: ((BaseQuickAdapter<*, *>, View, Int) -> Unit)? = null

    override fun bindData(
        holder: BaseViewHolder<List<String>>,
        data: List<String>?,
        position: Int,
        pageSize: Int
    ) {
        val mRvMenuItemAdapter = RvMenuItemAdapter()
        mRvMenuItemAdapter.setList(data)

        val menuRecyclerView = holder.findViewById<RecyclerView>(R.id.rv_menulist)
        menuRecyclerView.adapter = mRvMenuItemAdapter
//        menuRecyclerView.addItemDecoration(
//            MyDividerItem(
//                menuRecyclerView.dp2px(15f),
//                menuRecyclerView.dp2px(18f))
//        )

        menuRecyclerView.addItemDecoration(
            GridSpaceItemDecoration(
                4,
                menuRecyclerView.dp2px(18f),
                menuRecyclerView.dp2px(15f)
            )
        )
        mRvMenuItemAdapter.setOnItemClickListener { adapter, view, position ->
            //todo 方式1通过定义一个接口回传
//            this.mOnItemClickListener?.onItemClick(adapter, view, position)
            //todo 方式2 通过dsl 方式回调出去
//            if (::mListener.isInitialized)
//            {
//                mListener.mOnItemClick?.invoke(adapter, view, position)
//            }
            //todo 方式3 通过DSL 方式回调
            mOnItemClickCallBck?.invoke(adapter,view,position)
        }
    }


    override fun getLayoutId(viewType: Int): Int = R.layout.item_view_grid

    interface OnItemClickCallBack {
        /**
         * Callback method to be invoked when an item in this RecyclerView has
         * been clicked.
         *
         * @param adapter  the adapter
         * @param view     The itemView within the RecyclerView that was clicked (this
         * will be a view provided by the adapter)
         * @param position The position of the view in the adapter.
         */
        fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int)
    }

    /**
     *  //todo 方式2 通过dsl 方式回调出去
        if (::mListener.isInitialized)
        {
        mListener.mOnItemClick?.invoke(adapter, view, position)
        }
     */
    private lateinit var mListener: OnResultCallBackBuild
    //todo 2 注册的回调函数 外部实用注册函数
    fun registerListener(listenerBuilder: OnResultCallBackBuild.()->Unit) {
        //todo 带ListenerBuilder返回值的lamba
        mListener=OnResultCallBackBuild().also(listenerBuilder)
    }


    /**
     * todo  1 定义一个回调的Builder类，并且在类中定义回调lamba表达式对象成员，最后再定义Builder类的成员函数，这些函数就是暴露给外部回调的函数。
     * todo 个人习惯把它作为一个类的内部类
     */
    inner class OnResultCallBackBuild {
        //todo 定义内部回调的参数类型
        var mOnItemClick: ((BaseQuickAdapter<*, *>, View, Int) -> Unit)? = null
        //todo 外部的dsl 函数快
        fun onItemClick(action: (BaseQuickAdapter<*, *>, View, Int) -> Unit) {
            this.mOnItemClick = action
        }
    }



}